﻿// This software is the confidential and proprietary information of NetBay.
// Such confidential information should not be disclosed.
// NetBay copyright(c) 2012

#region

using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

#endregion

namespace NetBay.Core.Encryption.Licensing
{
    /// <summary>
    /// terms of the license agreement: it's not encrypted (but is obscured)
    /// </summary>
    [Serializable]
    [DataContract]
    public class LicenseTerms
    {
        #region Constructors

        public LicenseTerms()
        {
            Version = new VersionInfo();
        }

        #endregion

        #region Instance Properties

        /// <summary>
        /// the last date on which the software can be used on this license.
        /// </summary>
        [DataMember]        
        public DateTime EndDate { get; set; }

        /// <summary>
        /// Gets or sets the type of the license.
        /// </summary>
        /// <value>
        /// The type of the license.
        /// </value>
        [DataMember]    
        public LicenseType LicenseType { get; set; }

        /// <summary>
        /// the assembly name of the product that is licensed.
        /// </summary>
        [DataMember]    
        public string ProductName { get; set; }

        /// <summary>
        /// start date of the license agreement.
        /// </summary>
        [DataMember]    
        public DateTime StartDate { get; set; }

        /// <summary>
        /// The number of declared users.
        /// </summary>
        /// <value>
        /// The number.
        /// </value>
        [DataMember]
        public long NbUserDeclaredAllowed { get; set; }

        /// <summary>
        /// The number of documents in the base.
        /// </summary>
        /// <value>
        /// The number.
        /// </value>
        [DataMember]
        public long NbDocumentsAllowed { get; set; }

        /// <summary>
        /// Gets or sets the version.
        /// </summary>
        /// <value>
        /// The version.
        /// </value>
        [DataMember]    
        public VersionInfo Version { get; set; }


        /// <summary>
        /// The domain where the application is installed
        /// </summary>
        [DataMember]
        public string DomainName { get; set; }

        /// <summary>
        /// The client key
        /// </summary>
        [DataMember]
        public string ClientKey { get; set; }

        /// <summary>
        /// The reseller key
        /// </summary>
        [DataMember]
        public string ResellerKey { get; set; }

        /// <summary>
        /// The number of authorized installation of RioGed
        /// </summary>
        [DataMember]
        public long RioGedInstallLimit { get; set; }

        /// <summary>
        /// The password of the template file
        /// </summary>
        [DataMember]
        public string TemplatePassword { get; set; }
        
        /// <summary>
        /// The Hdd serial number
        /// </summary>
        [DataMember]
        public string HddSerialNumber { get; set; }

        /// <summary>
        /// The Hdd signature
        /// </summary>
        [DataMember]
        public string HddSignature { get; set; }

        /// <summary>
        /// The Mac adress
        /// </summary>
        [DataMember]
        public string MacAdress { get; set; }

        /// <summary>
        /// The Ip adress
        /// </summary>
        [DataMember]
        public string IpAdress { get; set; }

        /// <summary>
        /// The Processor Id
        /// </summary>
        [DataMember]
        public string ProcessorId { get; set; }
        
        #endregion

        #region Instance Methods

        /// <summary>
        /// returns a binary representation of the license terms.
        /// </summary>
        /// <returns></returns>
        public byte[] GetLicenseData()
        {
            using (MemoryStream ms = new MemoryStream())
            {
                // create a binary formatter:
                BinaryFormatter bnfmt = new BinaryFormatter();

                // serialize the data to the memory-steam;
                bnfmt.Serialize(ms, this);

                // return a base64 string representation of the binary data:
                return ms.GetBuffer();
            }
        }

        /// <summary>
        /// returns the license terms as an obscure (not human readable) string.
        /// </summary>
        /// <returns></returns>
        public String GetLicenseString()
        {
            using (MemoryStream ms = new MemoryStream())
            {
                // create a binary formatter:
                BinaryFormatter bnfmt = new BinaryFormatter();

                // serialize the data to the memory-steam;
                bnfmt.Serialize(ms, this);

                // return a base64 string representation of the binary data:
                return Convert.ToBase64String(ms.GetBuffer());
            }
        }

        #endregion

        #region Class Methods

        /// <summary>
        /// create a new license-terms object from a string-representation of the binary
        /// serialization of the licence-terms.
        /// </summary>
        /// <param name="licenseTerms"></param>
        /// <returns></returns>
        public static LicenseTerms FromString(String licenseTerms)
        {
            using (MemoryStream ms = new MemoryStream(Convert.FromBase64String(licenseTerms)))
            {
                // create a binary formatter:
                BinaryFormatter bnfmt = new BinaryFormatter();

                // serialize the data to the memory-steam;
                object value = bnfmt.Deserialize(ms);

                if (value is LicenseTerms)
                    return (LicenseTerms) value;
                else
                    throw new ApplicationException("Invalid Type!");
            }
        }

        #endregion
    }

    [Serializable]
    [DataContract]
    public class VersionInfo
    {
        #region Instance Properties

        /// <summary>
        /// Gets or sets the major.
        /// </summary>
        /// <value>
        /// The major.
        /// </value>
        [DataMember]    
        public int Major { get; set; }

        /// <summary>
        /// Gets or sets the minor.
        /// </summary>
        /// <value>
        /// The minor.
        /// </value>
        [DataMember]    
        public int Minor { get; set; }

        #endregion
    }
}